﻿package baidu.reader.common.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.ui.containers.*;
    import baidu.ui.controls.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    import flash.display.*;
    import flash.events.*;

    public class TopToolBar extends AutoLayoutContainer
    {
        private var _leftBox:HBox;
        private var _rightBox:HBox;
        private var _tocButton:Button;
        private var _zoomInButton:Button;
        private var _zoomOutButton:Button;
        private var _originalButton:Button;
        private var _settingsButton:Button;
        private var _singlePageButton:Button;
        private var _doublePageButton:Button;
        private var _fullscreenButton:Button;
        private var buttonsArray:Array;
        private var tooltipManager:TooltipManager;
        private var screenModel:ScreenModel;
        private var isCurrentSinglePage:Boolean = true;
        private var previousButtonState:Array;
        private var _displayTypeDisabled:Boolean;

        public function TopToolBar()
        {
            this.previousButtonState = [];
            this.tooltipManager = TooltipManager.getInstance();
            this.addLeftBox();
            this.addRightBox();
            this.buttonsArray = [this._tocButton, this._zoomInButton, this._zoomOutButton, this._originalButton, this._settingsButton, this._singlePageButton, this._doublePageButton, this._fullscreenButton];
            var _loc_1:* = new Tooltip();
            _loc_1.setStyle("skin", TipBackground);
            _loc_1.setStyle("padding", 5);
            this.tooltipManager.defaultRender = _loc_1;
            this.tooltipManager.register(this._tocButton, " 目录 ");
            this.tooltipManager.register(this._zoomInButton, " 放大 ");
            this.tooltipManager.register(this._zoomOutButton, " 缩小 ");
            this.tooltipManager.register(this._singlePageButton, " 单页 ");
            this.tooltipManager.register(this._doublePageButton, " 双页 ");
            this.tooltipManager.register(this._originalButton, " 还原 ");
            this.tooltipManager.register(this._settingsButton, " 设置 ");
            this.tooltipManager.register(this._fullscreenButton, " 全屏 ");
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(FullScreenEvent.FULL_SCREEN, this.screenResizedHandler);
            return;
        }// end function

        private function screenResizedHandler(event:Event) : void
        {
            switch(this.screenModel.stage.displayState)
            {
                case StageDisplayState.FULL_SCREEN:
                {
                    this.tooltipManager.register(this._fullscreenButton, " 退出全屏 ");
                    break;
                }
                default:
                {
                    this.tooltipManager.register(this._fullscreenButton, " 全屏 ");
                    break;
                }
            }
            return;
        }// end function

        private function addLeftBox() : void
        {
            this._leftBox = new HBox();
            this._leftBox.setStyle("gap", 12);
            addChild(this._leftBox);
            var _loc_1:* = _childsLayoutInfo[this._leftBox];
            _loc_1.top = 0;
            _loc_1.left = 0;
            this._tocButton = new Button();
            this._tocButton.setStyle("skin", ToC_Button_Skin);
            this._tocButton.useHandCursor = true;
            this._tocButton.addEventListener(MouseEvent.CLICK, this.tocClickHandler);
            this._leftBox.addChild(this._tocButton);
            this._zoomInButton = new Button();
            this._zoomInButton.setStyle("skin", ZoomIn_Button_Skin);
            this._zoomInButton.useHandCursor = true;
            this._zoomInButton.addEventListener(MouseEvent.CLICK, this.zoomInClickHandler);
            this._leftBox.addChild(this._zoomInButton);
            this._zoomOutButton = new Button();
            this._zoomOutButton.setStyle("skin", ZoomOut_Button_Skin);
            this._zoomOutButton.useHandCursor = true;
            this._zoomOutButton.addEventListener(MouseEvent.CLICK, this.zoomOutClickHandler);
            this._leftBox.addChild(this._zoomOutButton);
            this._originalButton = new Button();
            this._originalButton.setStyle("skin", Original_Button_Skin);
            this._originalButton.useHandCursor = true;
            this._originalButton.addEventListener(MouseEvent.CLICK, this.originalClickHandler);
            this._leftBox.addChild(this._originalButton);
            return;
        }// end function

        private function originalClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.ORIGINAL_SIZE));
            return;
        }// end function

        private function zoomOutClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.ZOOM_IN));
            return;
        }// end function

        private function zoomInClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.ZOOM_OUT));
            return;
        }// end function

        private function bookmarkClickHandler(event:MouseEvent) : void
        {
            return;
        }// end function

        private function tocClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.SHOW_TOC));
            return;
        }// end function

        private function addRightBox() : void
        {
            if (this._rightBox)
            {
                this._rightBox.parent.removeChild(this._rightBox);
            }
            this._rightBox = new HBox();
            this._rightBox.setStyle("gap", 12);
            this._rightBox.setStyle("padding", 12);
            addChild(this._rightBox);
            var _loc_1:* = _childsLayoutInfo[this._rightBox];
            _loc_1.top = 0;
            _loc_1.right = 0;
            this._settingsButton = new Button();
            this._settingsButton.setStyle("skin", Settings_Button_Skin);
            this._settingsButton.useHandCursor = true;
            this._settingsButton.addEventListener(MouseEvent.CLICK, this.settingsClickHandler);
            this._rightBox.addChild(this._settingsButton);
            this._singlePageButton = new Button();
            this._singlePageButton.setStyle("skin", SinglePage_Button_Skin);
            this._singlePageButton.useHandCursor = true;
            this._singlePageButton.addEventListener(MouseEvent.CLICK, this.singlePageClickHandler);
            this._rightBox.addChild(this._singlePageButton);
            this._doublePageButton = new Button();
            this._doublePageButton.setStyle("skin", DoublePage_Button_Skin);
            this._doublePageButton.useHandCursor = true;
            this._doublePageButton.addEventListener(MouseEvent.CLICK, this.doublePageClickHandler);
            this._rightBox.addChild(this._doublePageButton);
            this._fullscreenButton = new Button();
            this._fullscreenButton.setStyle("skin", FullScreen_Button_Skin);
            this._fullscreenButton.useHandCursor = true;
            this._fullscreenButton.addEventListener(MouseEvent.CLICK, this.fullScreenClickHandler);
            this._rightBox.addChild(this._fullscreenButton);
            return;
        }// end function

        public function disableAll() : void
        {
            var i:int;
            while (i < this.buttonsArray.length)
            {
                
                try
                {
                    this.previousButtonState[i] = (this.buttonsArray[i] as Button).enabled;
                    this.disableButton(this.buttonsArray[i] as Button);
                }
                catch (e:Error)
                {
                    trace(e.getStackTrace());
                }
                i = (i + 1);
            }
            return;
        }// end function

        public function enableAll() : void
        {
            var _loc_1:Button = null;
            for each (_loc_1 in this.buttonsArray)
            {
                
                this.enableButton(_loc_1);
            }
            return;
        }// end function

        public function restoreAll() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this.buttonsArray.length)
            {
                
                if (this.previousButtonState.length)
                {
                    (this.buttonsArray[_loc_1] as Button).enabled = this.previousButtonState[_loc_1];
                }
                _loc_1++;
            }
            return;
        }// end function

        private function disableButton(param1:Button) : void
        {
            param1.enabled = false;
            return;
        }// end function

        private function enableButton(param1:Button) : void
        {
            param1.enabled = true;
            return;
        }// end function

        private function fullScreenClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.TOGGLE_FULLSCREEN));
            return;
        }// end function

        private function doublePageClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_DOUBLE_PAGE));
            return;
        }// end function

        private function singlePageClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_SINGLE_PAGE));
            return;
        }// end function

        private function settingsClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.SHOW_SETTING_PANEL));
            return;
        }// end function

        public function get tocButton() : Button
        {
            return this._tocButton;
        }// end function

        public function get zoomInButton() : Button
        {
            return this._zoomInButton;
        }// end function

        public function get zoomOutButton() : Button
        {
            return this._zoomOutButton;
        }// end function

        public function get originalButton() : Button
        {
            return this._originalButton;
        }// end function

        public function get settingsButton() : Button
        {
            return this._settingsButton;
        }// end function

        public function get singlePageButton() : Button
        {
            return this._singlePageButton;
        }// end function

        public function get doublePageButton() : Button
        {
            return this._doublePageButton;
        }// end function

        public function set isSingleDisplay(param1:Boolean) : void
        {
            if (this._displayTypeDisabled)
            {
                return;
            }
            this.isCurrentSinglePage = param1;
            if (param1 == true)
            {
                this._singlePageButton.enabled = false;
                this._doublePageButton.enabled = true;
                this._zoomInButton.enabled = true;
                this._zoomOutButton.enabled = true;
                this._originalButton.enabled = true;
            }
            else
            {
                this._singlePageButton.enabled = true;
                this._doublePageButton.enabled = false;
                this._zoomInButton.enabled = false;
                this._zoomOutButton.enabled = false;
                this._originalButton.enabled = false;
            }
            return;
        }// end function

        public function set displayTypeDisabled(param1:Boolean) : void
        {
            this._displayTypeDisabled = param1;
            if (param1)
            {
                this._singlePageButton.parent.removeChild(this._singlePageButton);
                this._doublePageButton.parent.removeChild(this._doublePageButton);
            }
            else
            {
                this._rightBox.addChild(this._settingsButton);
                this._rightBox.addChild(this._singlePageButton);
                this._rightBox.addChild(this._doublePageButton);
                this._rightBox.addChild(this._fullscreenButton);
                this.isSingleDisplay = this.isCurrentSinglePage;
            }
            return;
        }// end function

        public function get fullscreenButton() : Button
        {
            return this._fullscreenButton;
        }// end function

        public function get rightBox() : HBox
        {
            return this._rightBox;
        }// end function

    }
}
